// 工具类js, 判断or数据处理
import _debounce from 'lodash/debounce';

// 判断浏览器类型, 1为移动端, 0为PC端
export const browserType = () => {
  let result;
  if (/Android|webOS|iPhone|iPod|ipad|BlackBerry/i.test(navigator.userAgent)) {
    result = 1;
  } else {
    result = 0;
  }
  return result;
}


// 移动端禁止滑动
export const noSliding = (option = null) => {
  let mo = (e = null) => {
    return
  };
  if (option) {
    $('body, html').css('overflow-y', 'hidden');
    document.addEventListener("touchmove", mo, false);
  } else {
    $('body, html').css('overflow-y', 'auto');
    document.removeEventListener("touchmove", mo, false);
  }
}

// 判断横竖屏并提醒
export const detectOrient = () => {
  let orientation = window.orientation;
  let pd = null;
  function createPd() {
    if (document.getElementById('preventTran') === null) {
      let imgData = '';
      pd = document.createElement('div');
      pd.setAttribute('id', 'preventTran');
      pd.style.position = 'fixed';
      pd.style.left = '0';
      pd.style.top = '0';
      pd.style.width = '100%';
      pd.style.height = '100%';
      pd.style.overflow = 'hidden';
      pd.style.backgroundColor = '#2e2e2e';
      pd.style.textAlign = 'center';
      pd.style.zIndex = '99999';
      document.getElementsByTagName('body')[0].appendChild(pd);
      let img = document.createElement('img');
      img.src = imgData;
      pd.appendChild(img);
      img.style.margin = '60px auto 30px'
      let br = document.createElement('br');
      let p = document.createElement('p');
      p.style.width = '100%';
      p.style.height = 'auto';
      p.style.fontSize = '22px';
      p.style.color = '#626262';
      p.style.lineHeight = '34px';
      p.style.textAlign = 'center';
      p.innerHTML = 'for your better experience';
      p.appendChild(br);
      p.innerHTML += 'Please operate the mobile phone / flat panel vertically';
      pd.appendChild(p);
    }
  }
  if (orientation == 90 || orientation == -90) {
    if (pd == null && document.getElementById('preventTran') === null) createPd();
    document.getElementById('preventTran').style.display = 'block';
    $('header').css('display', 'none');
  }
  window.onorientationchange = function () {
    if (pd == null && document.getElementById('preventTran') == null) createPd();
    document.getElementById('preventTran').style.display = 'none';
    $('header').css('display', 'block');
    detectOrient();
  };
}

// 导出lodash中的防抖函数
export const debounce = _debounce;